import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddCreatedAtToMatches1710313054000 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    // 检查列是否已存在
    const tableExists = await queryRunner.hasTable('matches');
    if (!tableExists) {
      console.log('表 matches 不存在，跳过迁移');
      return;
    }

    const columnExists = await queryRunner.hasColumn('matches', 'created_at');
    if (!columnExists) {
      await queryRunner.query(
        `ALTER TABLE matches ADD COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP`,
      );
      console.log('成功添加 created_at 列到 matches 表');
    } else {
      console.log('列 created_at 已存在，跳过迁移');
    }
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    const columnExists = await queryRunner.hasColumn('matches', 'created_at');
    if (columnExists) {
      await queryRunner.query(`ALTER TABLE matches DROP COLUMN created_at`);
      console.log('成功从 matches 表中删除 created_at 列');
    }
  }
}
